const _path = process.cwd();
import plugin from '../../lib/plugins/plugin.js'
import cfg from '../../lib/config/config.js';
import { segment } from "oicq"
import fetch from "node-fetch"
import schedule from "node-schedule"
import moment from "moment"
import fs from 'fs'
import { promisify } from "util"
import { pipeline } from "stream"

let name ="纳西妲" //机器人名字
let gailv = 20//全局文字回复概率 1～100
let gailv2 = 0.1//图片回复概率 0～1
let whilelist = [529331890] //开启全局回复的群白名单
var dirpath = "resources/ying";
if (!fs.existsSync(dirpath)) {//如果文件夹不存在
			fs.mkdirSync(dirpath);//创建文件夹
		  }
export class example extends plugin {
  constructor () {
    super({
      /** 功能名称 */
      name: '思知ai',
      /** 功能描述 */
      dsc: '简单开发示例',
      /** https://oicqjs.github.io/oicq/#events */
      event: 'message',
      /** 优先级，数字越小等级越高 */
      priority: 5000,
      rule: [
        {
          /** 命令正则匹配 */
          reg: '.+',
          /** 执行方法 */
          fnc: 'si'
        }, {
          /** 命令正则匹配 */
          reg: '#思知回复(.*)',
          /** 执行方法 */
          fnc: 'gailv'
        },{
		  /** 命令正则匹配 */
          reg: '#斗图回复(.*)',
          /** 执行方法 */
          fnc: 'gailv2'
}
      ]
    })
  }

 async gailv (e) {
	  if(e.msg.includes('#思知回复关闭') & e.isMaster){
		  gailv = 0
		  e.reply('思知回复已关闭')
	  }
	  let gail = e.msg.replace(/#思知回复概率/,'').trim();
if(e.msg == (`#思知回复概率${gail}`) & e.isMaster){		 
gailv = gail
console.log(gailv)
e.reply(`当前概率已调节为${gail}%`)
}
if(e.msg == (`#思知回复概率${gail}`) &!e.isMaster){		 
e.reply(`你不是主人，无法调节`)
}
}
  async gailv2 (e) {
	  if(e.msg.includes('#斗图回复关闭') & e.isMaster){
		  gailv = 0
		  e.reply('斗图回复已关闭')
	  }
	  let gail = e.msg.replace(/#斗图回复概率/,'').trim();
if(e.msg == (`#斗图回复概率${gail}`) & e.isMaster){		 
gailv2 = gail
console.log(gailv2)
e.reply(`当前概率已调节为${gail}%`)
}
if(e.msg == (`#斗图回复概率${gail}`) &!e.isMaster){		 
e.reply(`你不是主人，无法调节`)
}
}
  async si (e) {
	  let num2 = Math.random()
	  if(e.atBot && e.msg){ 
let num3 = Math.ceil(Math.random()* 2)
 if(num3 == 1){       
         answer(e)
}else{
answer2(e)
}
		 return true
     } 
if(num2 < gailv2){    
	  if (e.message.find(val => val.type === 'image')) { 
let url = encodeURI('http://ovooa.com/API/dou/?type=text')
let res = await fetch(url)
res = await res.text()

         e.reply([segment.image(res)])
		 return true
     } 
}
     if(!(e.message[0].type == 'text')){
      return true
    }
   
      if(whilelist.includes(e.group_id)){
		let num = Math.ceil(Math . random( )* 100)
          console.log( num)
           if(num <=gailv){
answer(e)
				  return false
			  }
			  
          return false;  
       
      }
  
   
  }
}

async function answer(e) {
    let url = `https://api.ownthink.com/bot?appid=xiaosi&userid=user&spoken=${e.msg}`
let res = await fetch(url)
       res = await res.text()
let regs = /[\u4e00-\u9fa5|\u3002|\uff1f|\uff01|\uff0c|\u3001|\uff1b|\uff1a|\u201c|\u201d|\u2018|\u2019|\uff08|\uff09|\u300a|\u300b|\u3008|\u3009|\d]/g
var str = `${res}`
var s = str.split("5000").join("")
var str1 = `${s}`
 let list = str1.replace(/小思/g,name)
var str2 = `${list}`
    let list2 = str2.match(regs);


        e.reply(list2)
}
async function answer2(e) {
function random(min, max) { 
return Math.floor(Math.random() * (max - min)) + min;
 }
for (var i = 1; i <=1 ; i++) { 
console.log(random(1, 999));
 }
    let url = `https://api.ownthink.com/bot?appid=xiaosi&userid=user&spoken=${e.msg}`
let res = await fetch(url)
       res = await res.text()
let regs = /[\u4e00-\u9fa5|\u3002|\uff1f|\uff01|\uff0c|\u3001|\uff1b|\uff1a|\u201c|\u201d|\u2018|\u2019|\uff08|\uff09|\u300a|\u300b|\u3008|\u3009|\d]/g
var str = `${res}`
var s = str.split("5000").join("")
var str1 = `${s}`
 let list = str1.replace(/小思/g,name)
var str2 = `${list}`
    let list2 = str2.match(regs);

let url2 = `http://ovooa.com/API/yuyin/api.php?msg=${list2}&type=text&speed=9`
let res2 = await fetch(url2)
res2 = await res2.text()
let response = await fetch(res2);
let buff = await response.arrayBuffer();
let now = random(1,999)
var filename = `${now}.mp3`;
fs.appendFileSync(`./resources/ying/${filename}`, Buffer.from(buff), "binary", );
e.reply([segment.record(dirpath + "/" + filename)])
       
        

}





















